package com.fuang.classload;

/**
 * Created by Fuang on 2017-9-23.
 * Class对象根据类加载器对象 + 类的全限定名确定的
 */
public class ClassLoaderTest {

    public static void main( String[ ] args) throws Exception {
        ClassLoader myLoader = new MyClassLoader(); //自定义类加载器实例1
        ClassLoader myLoader1 = new MyClassLoader(); //自定义类加载器实例2

        Object obj = myLoader.loadClass("com.fuang.classload.ClassLoaderTest").newInstance();
        Object object = myLoader1.loadClass("com.fuang.classload.ClassLoaderTest").newInstance();
        // Object object2 = myLoader1.loadClass("com.fuang.classload.ClassLoaderTest").newInstance(); // 报异常，一个类加载器不能加载两个一样的Class
        Object strObj = myLoader.loadClass("java.lang.String").newInstance();
        Object strObject = myLoader1.loadClass("java.lang.String").newInstance();

        System.out.println(obj.getClass());
        System.out.println(object.getClass());
        System.out.println(obj instanceof com.fuang.classload.ClassLoaderTest); // false 默认是使用AppClassLoader加载类，obj是myLoader加载，所以不等
        System.out.println(object instanceof com.fuang.classload.ClassLoaderTest); // false 默认是使用AppClassLoader加载类，object是myLoader1加载，所以不等
        System.out.println(myLoader.getClass().getClassLoader());
        System.out.println(obj.getClass().getClassLoader());
        System.out.println(object.getClass().getClassLoader());
        System.out.println(obj.getClass().equals(object.getClass())); // false myLoader类加载器和myLoader1类加载器加载，所以不等
        System.out.println(strObj.getClass().getClassLoader());
        System.out.println(strObj instanceof java.lang.String);
        System.out.println(strObj.getClass().equals(strObject.getClass()));
    }
}
